﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RobotEscape.Screens;
using System.Windows.Media.Imaging;
using RobotEscape.Entities;

namespace RobotEscape
{
    public class Hud
    {
        GameScreen gameScreen;
        Rectangle hudRect = new Rectangle();
        int framecounter = 0;
        double lastFPSCheckTime = 0;
        double fps = 0;

        public Hud(GameScreen _gameScreen)
        {
            this.gameScreen = _gameScreen;

            Canvas.SetLeft(hudRect, 0);
            Canvas.SetTop(hudRect, 0);
            this.hudRect.Width = Game.Instance.CANVAS_WIDTH;
            this.hudRect.Height = Game.Instance.CANVAS_HEIGHT;
            Game.Instance.Canvas.Children.Add(hudRect);
        }

        public void Update(GameTime time)
        {
            framecounter++;
            double elapsed = time.TotalGameTime.TotalMilliseconds - lastFPSCheckTime;
            if (elapsed >= 1000)
            {
                fps = framecounter / (elapsed / 1000);
                framecounter = 0;
                lastFPSCheckTime = time.TotalGameTime.TotalMilliseconds;
            }

            WriteableBitmap wb = new WriteableBitmap(Game.Instance.CANVAS_WIDTH + 1, Game.Instance.CANVAS_HEIGHT);
            wb.Invalidate();
            wb.FillRectangle(0, 0, Game.Instance.CANVAS_WIDTH + 1, 20, Color.FromArgb(255, 40, 40, 40));
            int iPlateCount = 0;
            foreach (Plate plate in gameScreen.CurrentLevel.Plates)
            {
                wb.FillEllipse(700 + (iPlateCount * 20), 5, 710 + (iPlateCount * 20), 15, plate.Activated ? plate.hudOnColor :  plate.hudOffColor);
                iPlateCount++;
            }
            Util.DrawText(wb, "ROBOT ESCAPE", 16, Game.Instance.CANVAS_WIDTH / 2, 0, Color.FromArgb(255, 200, 200, 200), TextAlignment.Center);
            Util.DrawText(wb, "LEVEL", 16, 10, 0, Colors.Yellow);
            Util.DrawText(wb, gameScreen.levelNum.ToString() + " of " + Game.Instance.LEVEL_COUNT, 16, 65, 0, Color.FromArgb(255, 200, 200, 200));

            string gametime = String.Format("{0:N0}:{1:d2}:{2:d3}", Math.Floor(gameScreen.timeSpent.TotalMinutes), gameScreen.timeSpent.Seconds, gameScreen.timeSpent.Milliseconds);
            Util.DrawText(wb, gametime, 16, Game.Instance.CANVAS_WIDTH - 100, 0, Color.FromArgb(255, 200, 200, 200), TextAlignment.Left);


            //Util.DrawText(wb, "FPS", 16, Game.Instance.CANVAS_WIDTH - 60, 0, Colors.Yellow, TextAlignment.Right);
            //Util.DrawText(wb, fps.ToString("N2"), 16, Game.Instance.CANVAS_WIDTH - 10, 0, Color.FromArgb(255, 200, 200, 200), TextAlignment.Right);
            wb.Invalidate();

            var imageBrush = new ImageBrush
            {
                Stretch = Stretch.None,
                AlignmentX = AlignmentX.Left,
                AlignmentY = AlignmentY.Top
            };
            imageBrush.ImageSource = wb;
            this.hudRect.Fill = imageBrush;
        }

        public void Remove()
        {
            if (Game.Instance.Canvas.Children.Contains(hudRect))
                Game.Instance.Canvas.Children.Remove(hudRect);
        }
    }
}
